#include <GL\freeglut.h>
#include <iostream>
#include "KMController.h"
#include "main.h"


KMController::KMController(AreaMap* map,float xRot, float yRot, float xPos, float yPos, float zPos)
{
	this->map = map;

	cam = new Camera(xRot, yRot, xPos, yPos, zPos);
	cam->clamp(map->map_width, map->map_depth);

	keyStates = new bool[256]; // Create an array of boolean values of length 256 (0-255) 
	keySpecialStates = new bool[256]; // Create an array of boolean values of length 256 (0-255) 

	for (int i = 0; i < 256; i++)
	{
		keyStates[i] = false;
		keySpecialStates[i] = false;
	}
}

void KMController::update()
{
	keyOperations();
	cam->clamp(map->map_width, map->map_depth);
}

void KMController::rotate()
{
	cam->rotation();
}


void KMController::translate()
{
	cam->translation(map);
}


void KMController::keyReleased (unsigned char key, int x, int y)
{

	// other stuff that happens when they keys are released goes here

	// 'e' key toggles wireframe mode on & off
	if(keyStates['e'] || keyStates['E']){
		toggleWireframe();
	}

	keyStates[key] = false;
}

void KMController::keyPressed (unsigned char key, int x, int y)
{
	keyStates[key] = true;

	// other stuff that happens when they keys are pressed goes here

	// Test if user pressed ESCAPE (ascii 27)
	if(keyStates[27]){
		exitScene(); // If so, exit the program
	}
}

void KMController::specialKeyReleased	(int key, int x, int y)
{
	keySpecialStates[key] = false;

	// other stuff that happens when they keys are released goes here

}

void KMController::specialKeyPressed (int key, int x, int y)
{
	keySpecialStates[key] = true;

	// other stuff that happens when they keys are pressed goes here
}

void KMController::keyOperations()
{
	// movement with WASD
	if(keyStates['w'] || keyStates['W']){
		cam->move(-1,2); 

	}else if(keyStates['s'] || keyStates['S']){
		cam->move(1,2); 
	}

	if(keyStates['a'] || keyStates['A']){
		cam->move(-1,0); 

	}else if(keyStates['d'] || keyStates['D']){
		cam->move(1,0); 
	}

	// movement with Arrow Keys
	if(keySpecialStates[GLUT_KEY_UP]){
		cam->move(-1,2); 

	}else if(keySpecialStates[GLUT_KEY_DOWN]){
		cam->move(1,2); 
	}

	if(keySpecialStates[GLUT_KEY_LEFT]){
		cam->move(-1,0); 

	}else if(keySpecialStates[GLUT_KEY_RIGHT]){
		cam->move(1,0); 
	}

	// Other possible keypresses go here
}

void KMController::mouseButtonEvent(int button, int state, int x, int y)
{
	// if a mouse button has been released or pressed
	std::cout<<"mouse button="<<button<<" "<<"state="<<state<<std::endl;
}

void KMController::mouseActiveMovement(int x, int y)
{
	// movement when there are no buttons pressed
}

void KMController::mousePassiveMovement(int x, int y)
{
	// movement when there are buttons pressed
}

void KMController::mouseFocusChange(int state)
{
	// if the mouse has entered or left the scene
	if (state == GLUT_LEFT)
		std::cout<<"cursor left window"<<std::endl;
	else if(state == GLUT_ENTERED)
		std::cout<<"cursor entered window"<<std::endl;
}



KMController::~KMController(void)
{
	delete cam;
}
